﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using WeiXinPF.DBUtility;
using GroupMessageModel = WeiXinPF.Model.weixin.groupMessage.wx_groupMessage;

namespace WeiXinPF.DAL.weixin.groupMessage
{
    public class wx_groupMessage
    {
        public GroupMessageModel Add(GroupMessageModel model)
        {
            const string sql = @"INSERT INTO dbo.wx_groupMessage
                                                    ( wId ,
                                                      title ,
                                                      messageType ,
                                                      media_id ,
                                                      createTime ,
                                                      createUid ,
                                                      isUpload ,
                                                      uploadTime ,
                                                      uploadUid
                                                    )
                                            VALUES  ( @wId ,
                                                      @title ,
                                                      @messageType ,
                                                      NULL ,
                                                      GETDATE() ,
                                                      @createUid ,
                                                      0 ,
                                                      NULL ,
                                                      NULL
                                                    );
                                            SELECT @@IDENTITY;";
            var sqlparams = new SqlParameter[]
            {
                new SqlParameter() {ParameterName = "@wId",SqlDbType = SqlDbType.Int,Value = model.wId},
                new SqlParameter() {ParameterName = "@title",SqlDbType = SqlDbType.NVarChar,Value = model.title},
                new SqlParameter() {ParameterName="@messageType",SqlDbType = SqlDbType.NVarChar,Value = model.messageType},
                new SqlParameter() {ParameterName = "@createUid",SqlDbType = SqlDbType.Int,Value = model.createUid},
            };

            var result = DbHelperSQL.GetSingle(sql, sqlparams);
            model.id = Convert.ToInt32(result);

            return model;
        }

        public GroupMessageModel Update(GroupMessageModel model)
        {
            return null;
        }

        public bool Delete(int id)
        {
            const string sql = @"DELETE FROM dbo.wx_groupMessage WHERE id=@id";
            var sqlparams = new SqlParameter[]
            {
                new SqlParameter() {ParameterName = "@id",SqlDbType = SqlDbType.Int,Value = id}
            };

            return DbHelperSQL.ExecuteSql(sql, sqlparams) > 0;
        }

        public GroupMessageModel Get(int id)
        {
            const string sql = "SELECT * FROM dbo.wx_groupMessage where id=@id";
            var sqlparams = new SqlParameter[]
            {
                new SqlParameter() {ParameterName = "@id",SqlDbType = SqlDbType.Int,Value = id}
            };
            var result = DbHelperSQL.Query(sql, sqlparams);
            if (result != null && result.Tables.Count > 0 && result.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(result.Tables[0].Rows[0]);
            }

            return null;
        }

        public List<GroupMessageModel> GetList(int wid, int pageSize, int pageIndex, string title, out int total)
        {
            var rtnModelList = new List<GroupMessageModel>();
            var sqlparams = new SqlParameter[]
            {
                new SqlParameter() {ParameterName = "@wid",SqlDbType = SqlDbType.Int,Value = wid},
                 new SqlParameter() {ParameterName = "@pageSize",SqlDbType = SqlDbType.Int,Value = pageSize},
                 new SqlParameter() {ParameterName = "@pageIndex",SqlDbType = SqlDbType.Int,Value = pageIndex},
                 new SqlParameter() {ParameterName = "@title",SqlDbType = SqlDbType.Int,Value = title},
                 new SqlParameter() {ParameterName = "@total",SqlDbType = SqlDbType.Int,Direction = ParameterDirection.Output},
            };

            var result = DbHelperSQL.Query("exec usp_GetGroupMessageByPage", sqlparams);
            total = Convert.ToInt32(sqlparams[4].Value);

            if (result != null && result.Tables.Count > 0 && result.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in result.Tables[0].Rows)
                {
                    rtnModelList.Add(DataRowToModel(row));
                }
            }

            return rtnModelList;
        }

        private GroupMessageModel DataRowToModel(DataRow row)
        {
            var rtnGroupMessageModel = new GroupMessageModel();
            rtnGroupMessageModel.createTime = row.Field<DateTime>("createTime");
            rtnGroupMessageModel.createUid = row.Field<int>("createUid");
            rtnGroupMessageModel.id = row.Field<int>("id");
            rtnGroupMessageModel.isUpload = row.Field<bool>("isUpload");
            rtnGroupMessageModel.media_id = row.Field<string>("media_id");
            rtnGroupMessageModel.messageType = row.Field<string>("messageType");
            rtnGroupMessageModel.title = row.Field<string>("title");
            rtnGroupMessageModel.uploadTime = row.Field<DateTime?>("uploadTime");
            rtnGroupMessageModel.uploadUid = row.Field<int>("uploadUid");
            rtnGroupMessageModel.wId = row.Field<int>("wId");

            return rtnGroupMessageModel;
        }
    }
}
